package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/10/20 21:36
 **/
public class Day48_5_Num234_IsPalindrome {
    class Solution {
        public boolean isPalindrome(ListNode head) {
            StringBuffer sb = new StringBuffer();
            for(ListNode x = head; x != null; x = x.next){
                sb.append(x.val);
            }
            String s1 = sb.toString();
            String s2 = sb.reverse().toString();
            return s1.equals(s2);
        }
    }
    class Solution2 {
        public boolean isPalindrome(ListNode head) {
            if(head == null || head.next == null) return true;
            ListNode newHead = reverse(head);
            while(head != null){
                if(head.val != newHead.val) return false;
                head = head.next;
                newHead = newHead.next;
            }
            return true;
        }
        private ListNode reverse(ListNode head){
            ListNode dummyHead = new ListNode(-1);
            while(head != null){
                ListNode cur = new ListNode(head.val);
                cur.next = dummyHead.next;
                dummyHead.next = cur;
                head = head.next;
            }
            return dummyHead.next;
        }
    }
}